package com.neusoft.hifly.dictionary.dict.api;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.neusoft.hifly.commons.exception.SystemErrorException;
import com.neusoft.hifly.commons.lang.LongUtil;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.core.utils.GlobalConstants;
import com.neusoft.hifly.dictionary.dict.entity.DictionaryGroupObj;
import com.neusoft.hifly.dictionary.dict.service.IDictionaryGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Title: 数据字典服务-文章资讯
 * <p>
 * Description: 数据字典组 接口
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@CrossOrigin
@RestController
@RequestMapping("/api/v1/dictionary/group")
@Api(value = "dictionaryGroup-api", description = "数据字典组接口")
public class DictionaryGroupApi {
	@Autowired(required = false)
	private IDictionaryGroupService dictionaryGroupService;

	/**
	 * 查询所有数据
	 *
	 * @param HttpServletRequest
	 *            request
	 * @return 集合
	 */
	@ApiOperation("查询数据字典组集合")
	@GetMapping(value = "/list")
	public ApiResponse<?> findAll() {
		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！", dictionaryGroupService.findAll());
	}

	/**
	 * 分页查询
	 *
	 * @param HttpServletRequest
	 *            request
	 * @return 集合
	 */
	@ApiOperation("分页查询")
	@ApiImplicitParams({ @ApiImplicitParam(name = "offset", value = "开始索引", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "limit", value = "每页纪录数", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query") })
	@GetMapping(value = "/search")
	public ApiResponse<?> search(final HttpServletRequest request) {
		final String offsetStr = StringUtils.trimToEmpty(request.getParameter("offset"));
		final String limitStr = StringUtils.trimToEmpty(request.getParameter("limit"));
		int offset = GlobalConstants.PAGE_OFFSET;
		int limit = GlobalConstants.PAGE_LIMIT;
		if (StringUtils.isNotEmpty(offsetStr) && StringUtils.isNumeric(offsetStr)) {
			offset = Integer.parseInt(offsetStr);
		}
		if (StringUtils.isNotEmpty(limitStr) && StringUtils.isNumeric(limitStr)) {
			limit = Integer.parseInt(limitStr);
		}

		// 参数
		final Map<String, Object> params = new HashMap<String, Object>(0);
		// 添加搜索
		searchWhere(request, params);

		final IPage<DictionaryGroupObj> page = dictionaryGroupService.search(offset, limit, params);

		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！", page);
	}

	/**
	 * 通过逻辑ID获取数据字典组
	 *
	 * @param logicId
	 *            数据字典组逻辑ID
	 * @return 数据字典组信息
	 */
	@ApiOperation("通过逻辑ID获取数据字典组")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "logicId", value = "数据字典组逻辑ID", dataType = "long", paramType = "path", required = true) })
	@GetMapping(value = "/{logicId}")
	public ApiResponse<?> getByLogicId(@PathVariable("logicId") final long logicId) {
		if (LongUtil.isEmpty(logicId)) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误!");
		}
		final DictionaryGroupObj obj = dictionaryGroupService.getByLogicId(logicId);
		if (obj == null) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "数据不存在！");
		}
		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！", obj);
	}

	/**
	 * 通过逻辑ID删除数据字典组
	 *
	 * @param logicId
	 *            数据字典组逻辑ID
	 * @return 成功信息
	 */
	@ApiOperation("通过逻辑ID删除数据字典组")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "logicId", value = "数据字典组逻辑ID，多id使用英文逗号分隔", dataType = "string", paramType = "path", required = true) })
	@DeleteMapping(value = "/{logicId}")
	public ApiResponse<?> delete(@PathVariable("logicId") final String logicId) {
		if (StringUtils.isEmpty(logicId)) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误!");
		}
		dictionaryGroupService.deleteByBatch(logicId);
		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！");
	}

	/**
	 * 初始化编辑
	 *
	 * @param logicId
	 *            数据字典组逻辑ID
	 * @return 数据字典组信息
	 */
	@ApiOperation("初始化编辑")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "logicId", value = "数据字典组逻辑ID", dataType = "long", paramType = "path", required = true) })
	@GetMapping(value = "/initEdit/{logicId}")
	public ApiResponse<?> initEdit(@PathVariable("logicId") final long logicId) {
		if (LongUtil.isEmpty(logicId)) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误!");
		}
		final DictionaryGroupObj obj = dictionaryGroupService.getByLogicId(logicId);
		if (obj == null) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "数据不存在！");
		}
		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！", obj);
	}

	/**
	 * 新增数据字典组
	 *
	 * @param obj
	 *            数据字典组信息
	 * @return 数据字典组信息
	 */
	@ApiOperation("新增数据字典组")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "obj", value = "数据字典组信息", dataType = "DictionaryGroupObj", required = true) })
	@PostMapping(value = "")
	public ApiResponse<?> save(@RequestBody final DictionaryGroupObj obj) {

		if (obj == null) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误!");
		}
		try {
			dictionaryGroupService.saveOrUpdate(obj);
		} catch (final SystemErrorException e) {
			return new ApiResponse<>(e);
		}
		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！");
	}

	/**
	 * 更新数据字典组
	 *
	 * @param obj
	 *            数据字典组信息
	 * @return 数据字典组信息
	 */
	@ApiOperation("更新数据字典组")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "obj", value = "数据字典组信息", dataType = "DictionaryGroupObj", required = true) })
	@PutMapping(value = "")
	public ApiResponse<?> update(@RequestBody final DictionaryGroupObj obj) {
		if (obj == null) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误!");
		}
		final DictionaryGroupObj oldObj = dictionaryGroupService.getByLogicId(obj.getLogicId());
		if (oldObj == null) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "数据字典组不存在！");
		}
		try {
			dictionaryGroupService.saveOrUpdate(obj);
		} catch (final SystemErrorException e) {
			return new ApiResponse<>(e);
		}
		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！");
	}

	/**
	 * 搜索条件
	 *
	 * @param request
	 *            HttpServletRequest
	 * @param property
	 *            参数
	 */
	private void searchWhere(final HttpServletRequest request, final Map<String, Object> property) {
		boolean isLike = false;
		final String name = StringUtils.trimToEmpty(request.getParameter("name"));
		if (StringUtils.isNotEmpty(name)) {
			property.put("name", name);
			isLike = true;
		}
		final String code = StringUtils.trimToEmpty(request.getParameter("code"));
		if (StringUtils.isNotEmpty(code)) {
			property.put("code", code);
			isLike = true;
		}
		property.put("isLike", isLike);
	}
}